package com.hb.trade.api.im.main;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hb.core.pojo.enums.TradeWebsocketMsgEnum;
import com.hb.trade.api.im.hall.handler.HallMessageHandler;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.websocket.config.WebSocketSession;
import org.springframework.stereotype.Component;

import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.util.Objects;

/**
 * 系统页面
 * websocket Session
 */
@Slf4j
@Component
@ServerEndpoint("/trade/system/{uname}")
public class TradeSystemWebSocketSession extends TradeWebsocketSession {

    /***
     * 接收消息
     * @param message
     * @param session
     * @return
     */
    @OnMessage
    @Override
    public Integer onMessage(String message, Session session) {
        Integer infoType = super.getInfoType(message, session);
        if (infoType == null) {
            return null;
        }
        JSONObject msgJSON = JSONUtil.parseObj(message);
        // 根据消息类型判断处理逻辑
        switch (infoType) {
            case 1:
                //系统消息
                break;
            case 2:
                //即时通讯消息
                JSONObject dataContent = msgJSON.getJSONObject("dataContent");
                if (dataContent == null) {
                    log.warn("消息内容不能为空");
                    return null;
                }
                HallMessageHandler.handleIm(dataContent);
                break;
            //10 - 27 不能插入其他业务 只能是开标大厅
            case 10:
                //开标大厅签到开始
            case 11:
                //开标大厅开标开始
            case 12:
                //开标大厅解密开始
            case 13:
                //开标大厅解密结束
            case 14:
                //开标大厅唱标开始
            case 15:
                //开标大厅确认开始
            case 16:
                //开标大厅确认结束
            case 17:
                //开标大厅开标结束
            case 18:
                //开标大厅流标
            case 20:
                //投标人签到
            case 21:
                //投标人解密
            case 22:
                //投标人确认
            case 27:
                //播放会场纪律
                HallMessageHandler.handleHall(infoType, msgJSON);
                break;
            case 28:
                //发送系统消息  评标系统选举组长
                HallMessageHandler.sendSystem(infoType, msgJSON);
                break;
            case 29:
                //投标人再次报价(专家获取报价的列表)
                HallMessageHandler.supplierQuoteAgain(infoType, msgJSON);
                break;
            case 30:
                //专家发起二轮报价(投标人接收消息 填写报价)
                HallMessageHandler.expertSendQuoteMsg(infoType, msgJSON);
                //专家发起二轮报价(刷新报价项 如:第一轮报价 第二轮报价.....)
                HallMessageHandler.supplierQuoteAgain(TradeWebsocketMsgEnum.EXPERT_QUOTE_TERM.getCode(), msgJSON);
                break;

            default:
                break;
        }
        return null;
    }

    /***
     * 复写Equals方法
     * @param o
     * @return
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        if (!super.equals(o)) {
            return false;
        }
        TradeSystemWebSocketSession that = (TradeSystemWebSocketSession) o;
        String sectionId = getSectionId();
        String thatSectionId = that.getSectionId();
        return Objects.equals(uname, that.uname) && Objects.equals(sectionId, thatSectionId);
    }

}
